<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\adminapi\service;

use think\facade\Config;

/**
 * 数据处理
 * Class DataHandleService
 * @package app\adminapi\service
 */
class DataHandleService {

    /**
     * 数组求和
     */
    public static function array_sum_total($arr, $key, $count, $addend = 0) {
        $total_arr = array_column($arr, $key);
        if ($count > 0) {
            $total = array_sum($total_arr) / $count;
        } else {
            $total = array_sum($total_arr) * 0;
        }
        $total = $total + $addend * 1;
        return sprintf("%.6f", $total);
    }

    /**
     * 求和，并保留$mamy位小数
     */
    public static function array_add($array, $many) {
        return 1 * sprintf("%.{$many}f", array_sum($array));
    }

    /**
     * 求平均值，并保留$mamy位小数
     */
    public static function array_average($array, $many = 2) {
        $num = array_sum($array) / (count($array) == 0 ? 1 : count($array));
        return 1 * sprintf("%.{$many}f", $num * 1);
    }

    /**
     * 获取字符串首个汉字
     */
    public static function getStringFirstKeySort($string, $num = 1) {
        if (empty($string)) {
            return '*';
        }
        if (empty(mb_substr($string, ($num * 1 - 1), $num * 1, 'UTF-8'))) {
            return '*';
        }

        $asc = ord(mb_substr($string, ($num * 1 - 1), $num * 1, 'UTF-8'));
        if ($asc >= 0xD8 && $asc <= 0xF9) {
            return mb_substr($string, ($num * 1 - 1), $num * 1, 'UTF-8');
        } else {
            return self::getStringFirstKeySort($string, ++$num);
        }
    }

    public static function getQuarterTime($year, $month) {
        $m_arr = [];
        $m_arr['01'] = [$year . '01', $year . '02', $year . '03'];
        $m_arr['02'] = [$year . '01', $year . '02', $year . '03'];
        $m_arr['03'] = [$year . '01', $year . '02', $year . '03'];
        $m_arr['04'] = [$year . '04', $year . '05', $year . '06'];
        $m_arr['05'] = [$year . '04', $year . '05', $year . '06'];
        $m_arr['06'] = [$year . '04', $year . '05', $year . '06'];
        $m_arr['07'] = [$year . '07', $year . '08', $year . '09'];
        $m_arr['08'] = [$year . '07', $year . '08', $year . '09'];
        $m_arr['09'] = [$year . '07', $year . '08', $year . '09'];
        $m_arr['10'] = [$year . '10', $year . '11', $year . '12'];
        $m_arr['11'] = [$year . '10', $year . '11', $year . '12'];
        $m_arr['12'] = [$year . '10', $year . '11', $year . '12'];

        return $m_arr;
    }

    public static function getQuarterMonth($m = null) {
        $m_arr = [];
        $m_arr['01'] = "01+02+03月";
        $m_arr['02'] = "01+02+03月";
        $m_arr['03'] = "01+02+03月";
        $m_arr['04'] = "04+05+06月";
        $m_arr['05'] = "04+05+06月";
        $m_arr['06'] = "04+05+06月";
        $m_arr['07'] = "07+08+09月";
        $m_arr['08'] = "07+08+09月";
        $m_arr['09'] = "07+08+09月";
        $m_arr['10'] = "10+11+12月";
        $m_arr['11'] = "10+11+12月";
        $m_arr['12'] = "10+11+12月";

        if (empty($m)) {
            return $m_arr;
        } else {
            return $m_arr[$m];
        }
    }

}
